/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.inventory;

import action.report.Inventory;
import au.com.createpdf.CreateInventoryReport;
import com.lowagie.text.DocumentException;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import database.InventoryDAO;
import database.MoreInventoryDAO;
import database.StaffAuthenticationDAO;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import model.Product;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Inventory")
public class OrderAndReceiptAction {
     private List<Product> products = new ArrayList<Product>();
     private List<Inventory> inventories = new ArrayList<Inventory>();
     public MoreInventoryDAO dao = new MoreInventoryDAO();

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public List<Inventory> getInventories() {
        return inventories;
    }

    public void setInventories(List<Inventory> inventories) {
        this.inventories = inventories;
    }
     
     @Action(value = "inventory-statistic", results = {
        @Result(name = "success", location = "/manageInventory/OrderAndReceip.jsp")
    })
    public String OrderManagement() throws IOException, DocumentException {
        
        Map session = ActionContext.getContext().getSession();
        Staff staff = (Staff) session.get("staff");
        session.put("product", 1); 
        products = new InventoryDAO().getProductForInventory(1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        
        int[] idProduct = new int[products.size()];
        int i = 0;
        for(Product item: products)
        {
            idProduct[i] = item.getIdProduct();
            i++;
        }
        session.put("idProduct", idProduct);
        
        for (Product item: products)
        {
            
            Date order = dao.OrderTime(item.getName());
            Date today = new Date();
            //Date receip = dao.ReceiptTime(item.getName());
            String internalTime;
            String orderStatus;
            
            long diff;
            if(order.equals(new Date(0,0,0)))
            {
                diff = (today.getTime() - dao.ReceiptTime(item.getName()).getTime());
                orderStatus = "No order";
            }
            else
            {
                diff = (today.getTime() -  order.getTime());// - receip.getTime();
                orderStatus = dateFormat.format(order.getTime());
            }

            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
            if(diffDays  < 1)
                internalTime = diffHours + " hours "+ diffMinutes +" minutes";
            else 
                 internalTime = (diffDays) + " days";
            inventories.add(
//                    (String productName, String productCode, int stockQuantity, String timeBetween, String lastOrder, long interval)
                    new Inventory(item.getIdProduct(),item.getName(), item.getCode(), item.getStockQuantity(), internalTime, orderStatus, diff));
                    //new Inventory(item.getName(), item.getCode() ,item.getStockQuantity(),order , diff,internalTime));
        }
        //session.put("idProduct", idProduct);
        Collections.sort(inventories, Collections.reverseOrder());
        
        new StaffAuthenticationDAO().addAction("Inventory Statistic", staff.getIdStaff());
        return SUCCESS;
    }
}
